﻿using Microsoft.AspNetCore.Mvc;
using QntPlatform.Model;
using QntPlatform.Strategy.S2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace QntSvc.Web.Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RobotController : ApiCtrlBase<RobotInfo>
    {
        public bool EditStrategyArgs(string robotId, ArgInfo[] args)
        {
            var dic = new Dictionary<string, string>();
            foreach (var item in args)
            {
                dic.Add(item.Name, item.DefaultVal);
            }
            var robot = mng.GetById(robotId);
            robot.SetStrategyArgs(dic);
            return mng.Edit(robot);
        }
        public List<string> EditStrategyListArgs(Dictionary<string, Dictionary<string,string>> dic)
        {
            var reList = new List<string>();
            foreach (var item in dic)
            {
               var infos= item.Value.Select(p => new ArgInfo() { DefaultVal = p.Value, Name = p.Key }).ToArray();
                var re = EditStrategyArgs(item.Key, infos);
                if (!re)
                {
                    reList.Add(item.Key);
                }

            }
            return reList;
        }

        public RobotInfo[] Query(RobotInfo info) {

            //Expression<Func<RobotInfo, bool>> where = p => p.ExchangeId == info.ExchangeId;
            if (string.IsNullOrWhiteSpace(info.strategyId))
            {
             return   mng.GetList(p => p.ExchangeId == info.ExchangeId && p.Pair == info.Pair,1,int.MaxValue,total:out _);
            }
          return  mng.GetList(p => p.strategyId == info.strategyId && p.ExchangeId == info.ExchangeId && p.Pair == info.Pair,1,int.MaxValue,total:out _);
        }



    }
}
